using System;
using System.Collections.Generic;
using System.Text;

namespace [%NAMESPACE%]
{
    public class HyperLink : ContainerBase
    {
        #region Constractor
        public HyperLink(ItemBase p_parentItem, string p_fieldName, bool p_emptyAllowed)
            : base(p_parentItem, p_fieldName, p_emptyAllowed)
        {
            if (p_parentItem == null) throw new PointBridgeEx("HyperLink constructor: p_parentItem is null.");
            if (p_fieldName == null) throw new PointBridgeEx("HyperLink constructor: p_fieldName is null.");

            _url = null;
            _description = null;
        }

        #endregion

        #region Methods
        public override void SetEmpty()
        {
            _url = null;
            _description = null;
            ForceUpdate();
        }
        public void ChangeLink(string newUrl, string newDescription)
        {
            _url = newUrl;
            _description = newDescription;
            ForceUpdate();
        }
        public override void ForceUpdate()
        {
            parentItem.item[fieldName] = ToSPRepresentation();
        }
        public override void InitFromSPRepresentation(object obj)
        {
            if (obj == null)
            {
                if (emptyAllowed == true)
                {
                    _url = null;
                    _description = null;
                    return;
                }
                else
                {
                    throw new NullEx("Value is null. Use [Property Name]Nullable property if this is intended.");
                }
            }
            string hyperlinkValue = (string)obj;
            if (hyperlinkValue == "#ERR")
            {
                _url = null;
                _description = null;
                return;
            }

            string[] separators = new string[1];
            separators[0] = ", ";
            string[] splits = hyperlinkValue.Trim().Split(separators, StringSplitOptions.None);
            if (splits.Length != 2) throw new PointBridgeEx("Url string ('" + hyperlinkValue + "') is in invalid format");
            _url = splits[0];
            _description = splits[1];
        }
        public override object ToSPRepresentation()
        {
            if (isEmpty == true)
            {
                if (emptyAllowed == true) return null;
                else throw new PointBridgeEx("HyperLink.ToSPRepresentation(): object is not initialized while emptyAllowed is false");
            }
            return _url + ", " + _description;
        }
        public override string ToString()
        {
            if (isEmpty == true) return "<Empty>";
            else return (string)ToSPRepresentation();
        }
        #endregion

        #region Public Properties
        public override bool isEmpty
        {
            get
            {
                if (_url == null || _description == null) return true;
                else return false;
            }
        }

        public string url
        {
            get
            {
                if (isEmpty == true) throw new PointBridgeEx("class HyperLink: Attempting to access 'url' while this class container is empty.");
                return _url;
            }
        }
        public string description
        {
            get
            {
                if (isEmpty == true) throw new PointBridgeEx("class HyperLink: Attempting to access 'description' while this class container is empty.");
                return _description;
            }
        }
        #endregion

        #region Internal fields
        private string _url = null;
        private string _description = null;
        #endregion
    }
}
